package ru.silviasaint.fridge.server.handler;

import java.sql.SQLException;

import ru.silviasaint.fridge.server.bean.Product;
import ru.silviasaint.fridge.server.bean.impl.Data;
import ru.silviasaint.fridge.server.dao.ProductDao;
import ru.silviasaint.fridge.shared.bean.ProductSkinny;
import ru.silviasaint.fridge.shared.ingredients.AddProductAction;
import ru.silviasaint.fridge.shared.ingredients.AddProductResult;

import com.google.inject.Inject;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;

public class AddProductActionHandler implements ActionHandler<AddProductAction,AddProductResult>{
	@Inject ProductDao productDao;

	@Override
	public AddProductResult execute(AddProductAction action, ExecutionContext context)
			throws ActionException {
		Product product = new Product();
		product.setOriginalText(action.getName());
		try {
			productDao.create(product);
			Data data = new Data();
			data.setSubstance(product);
			AddProductResult result = new AddProductResult();
			result.setProduct((ProductSkinny) product.convertToSkinny());
			return result;
		} catch (SQLException e) {
			throw new ActionException(e);
		}
	}

	@Override
	public Class<AddProductAction> getActionType() {
		return null;
	}

	@Override
	public void undo(AddProductAction arg0, AddProductResult arg1,
			ExecutionContext arg2) throws ActionException {

	}

}
